<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="工序ID" prop="processId">
        <el-input
          v-model="queryParams.processId"
          placeholder="请输入工序ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="顺序编号" prop="orderNum">
        <el-input
          v-model="queryParams.orderNum"
          placeholder="请输入顺序编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="辅助设备" prop="device">
        <el-input
          v-model="queryParams.device"
          placeholder="请输入辅助设备"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="辅助材料" prop="material">
        <el-input
          v-model="queryParams.material"
          placeholder="请输入辅助材料"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="材料URL" prop="docUrl">
        <el-input
          v-model="queryParams.docUrl"
          placeholder="请输入材料URL"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="预留字段1" prop="attr1">
        <el-input
          v-model="queryParams.attr1"
          placeholder="请输入预留字段1"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预留字段2" prop="attr2">
        <el-input
          v-model="queryParams.attr2"
          placeholder="请输入预留字段2"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预留字段3" prop="attr3">
        <el-input
          v-model="queryParams.attr3"
          placeholder="请输入预留字段3"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预留字段4" prop="attr4">
        <el-input
          v-model="queryParams.attr4"
          placeholder="请输入预留字段4"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['produce:content:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['produce:content:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['produce:content:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['produce:content:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="contentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="内容ID" align="center" prop="contentId" />
      <el-table-column label="工序ID" align="center" prop="processId" /> -->
      <el-table-column label="顺序编号" align="center" prop="orderNum" />
      <el-table-column label="内容说明" align="center" prop="contentText" />
      <el-table-column label="辅助设备" align="center" prop="device" />
      <el-table-column label="辅助材料" align="center" prop="material" />
      <el-table-column label="材料URL" align="center" prop="docUrl" />
      <el-table-column label="备注" align="center" prop="remark" />
      <!-- <el-table-column label="预留字段1" align="center" prop="attr1" />
      <el-table-column label="预留字段2" align="center" prop="attr2" />
      <el-table-column label="预留字段3" align="center" prop="attr3" />
      <el-table-column label="预留字段4" align="center" prop="attr4" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['produce:content:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['produce:content:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改生产工序内容对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="工序ID" prop="processId">
          <el-input v-model="form.processId" placeholder="请输入工序ID" />
        </el-form-item> -->
        <el-form-item label="顺序编号" prop="orderNum">
          <el-input v-model="form.orderNum" placeholder="请输入顺序编号" />
        </el-form-item>
        <el-form-item label="内容说明" prop="contentText">
          <el-input v-model="form.contentText" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="辅助设备" prop="device">
          <el-input v-model="form.device" placeholder="请输入辅助设备" />
        </el-form-item>
        <el-form-item label="辅助材料" prop="material">
          <el-input v-model="form.material" placeholder="请输入辅助材料" />
        </el-form-item>
        <el-form-item label="材料URL" prop="docUrl">
          <el-input v-model="form.docUrl" placeholder="请输入材料URL" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <!-- <el-form-item label="预留字段1" prop="attr1">
          <el-input v-model="form.attr1" placeholder="请输入预留字段1" />
        </el-form-item>
        <el-form-item label="预留字段2" prop="attr2">
          <el-input v-model="form.attr2" placeholder="请输入预留字段2" />
        </el-form-item>
        <el-form-item label="预留字段3" prop="attr3">
          <el-input v-model="form.attr3" placeholder="请输入预留字段3" />
        </el-form-item>
        <el-form-item label="预留字段4" prop="attr4">
          <el-input v-model="form.attr4" placeholder="请输入预留字段4" />
        </el-form-item> -->
        <el-divider content-position="center">工序设置信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddProProcess">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteProProcess">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="proProcessList" :row-class-name="rowProProcessIndex" @selection-change="handleProProcessSelectionChange" ref="proProcess">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="工序编码" prop="processCode" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.processCode" placeholder="请输入工序编码" />
            </template>
          </el-table-column>
          <el-table-column label="工序名称" prop="processName" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.processName" placeholder="请输入工序名称" />
            </template>
          </el-table-column>
          <el-table-column label="是否启用" prop="enableFlag" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.enableFlag" placeholder="请输入是否启用" />
            </template>
          </el-table-column>
          <!-- <el-table-column label="预留字段1" prop="attr1" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.attr1" placeholder="请输入预留字段1" />
            </template>
          </el-table-column>
          <el-table-column label="预留字段2" prop="attr2" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.attr2" placeholder="请输入预留字段2" />
            </template>
          </el-table-column>
          <el-table-column label="预留字段3" prop="attr3" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.attr3" placeholder="请输入预留字段3" />
            </template>
          </el-table-column>
          <el-table-column label="预留字段4" prop="attr4" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.attr4" placeholder="请输入预留字段4" />
            </template>
          </el-table-column> -->
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listContent, getContent, delContent, addContent, updateContent } from "@/api/produce/content";

export default {
  name: "Content",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedProProcess: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产工序内容表格数据
      contentList: [],
      // 工序设置表格数据
      proProcessList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        processId: null,
        orderNum: null,
        contentText: null,
        device: null,
        material: null,
        docUrl: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        processId: [
          { required: true, message: "工序ID不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询生产工序内容列表 */
    getList() {
      this.loading = true;
      listContent(this.queryParams).then(response => {
        this.contentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        contentId: null,
        processId: null,
        orderNum: null,
        contentText: null,
        device: null,
        material: null,
        docUrl: null,
        remark: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.proProcessList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.contentId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加生产工序内容";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const contentId = row.contentId || this.ids
      getContent(contentId).then(response => {
        this.form = response.data;
        this.proProcessList = response.data.proProcessList;
        this.open = true;
        this.title = "修改生产工序内容";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.proProcessList = this.proProcessList;
          if (this.form.contentId != null) {
            updateContent(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addContent(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const contentIds = row.contentId || this.ids;
      this.$modal.confirm('是否确认删除生产工序内容编号为"' + contentIds + '"的数据项？').then(function() {
        return delContent(contentIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 工序设置序号 */
    rowProProcessIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 工序设置添加按钮操作 */
    handleAddProProcess() {
      let obj = {};
      obj.processCode = "";
      obj.processName = "";
      obj.attention = "";
      obj.enableFlag = "";
      obj.remark = "";
      obj.attr1 = "";
      obj.attr2 = "";
      obj.attr3 = "";
      obj.attr4 = "";
      this.proProcessList.push(obj);
    },
    /** 工序设置删除按钮操作 */
    handleDeleteProProcess() {
      if (this.checkedProProcess.length == 0) {
        this.$modal.msgError("请先选择要删除的工序设置数据");
      } else {
        const proProcessList = this.proProcessList;
        const checkedProProcess = this.checkedProProcess;
        this.proProcessList = proProcessList.filter(function(item) {
          return checkedProProcess.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleProProcessSelectionChange(selection) {
      this.checkedProProcess = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('produce/content/export', {
        ...this.queryParams
      }, `content_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
